﻿using DG.Tweening;
using System;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace BToolkit.ExeUtils
{
    public class Screensaver : MonoBehaviour
    {
        public TextureLoaderVideo video, loading;
        public float waitSecond = 30;
        public bool useAnim = true;
        public bool canTouchHide = true;
        [Space]
        public UnityEvent onShow, onHide;

        public static Screensaver instance;
        public bool isShow { get { return video.gameObject.activeInHierarchy; } }
        float timer;

        void Awake()
        {
            instance = this;

            if (Config.Instance.UseConfigScreenSize)
            {
                Vector2 screenSize = new Vector2(Config.Instance.ScreenW, Config.Instance.ScreenH);
                GetComponent<CanvasScaler>().referenceResolution = screenSize;
            }

            video.gameObject.SetActive(true);
            if (loading) loading.gameObject.SetActive(true);
            waitSecond = Config.Instance.ScreensaverWait;
            if (waitSecond > 0)
            {
                video.onLoadedAction = () =>
                {
                    video.onLoadedAction = null;
                    if (loading) Destroy(loading.gameObject);
                    if (!Config.Instance.ScreensaverDefaultShow)
                    {
                        Show(false, null);
                    }
                };
            }
            else
            {
                gameObject.SetActive(false);
            }
        }

        void Update()
        {
            if (Input.GetMouseButtonDown(0))
            {
                timer = Time.realtimeSinceStartup + waitSecond;
                if (canTouchHide && isShow)
                {
                    Show(false, null);
                }
            }

            if (timer > 0)
            {
                if (timer < Time.realtimeSinceStartup)
                {
                    timer = 0;
                    Show(true, null);
                }
            }
        }

        public void Show(bool b, Action onAnimEnd)
        {
            if (video.isLoaded)
            {
                if (b)
                {
                    video.gameObject.SetActive(true);
                    video.PlayVideo(true, null);
                    if (useAnim)
                    {
                        video.transform.localScale = Vector3.one * 5;
                        video.transform.DOScale(Vector3.one, 0.5f).SetEase(Ease.OutExpo).OnComplete(() =>
                        {
                            onAnimEnd?.Invoke();
                            onShow?.Invoke();
                        });
                    }
                    else
                    {
                        onAnimEnd?.Invoke();
                        onShow?.Invoke();
                    }
                }
                else
                {
                    if (useAnim)
                    {
                        video.transform.DOScale(Vector3.one * 5, 0.3f).SetEase(Ease.InExpo).OnComplete(() =>
                        {
                            video.transform.localScale = Vector3.one;
                            video.gameObject.SetActive(false);
                            timer = Time.realtimeSinceStartup + waitSecond;
                            onAnimEnd?.Invoke();
                            onHide?.Invoke();
                        });
                    }
                    else
                    {
                        video.gameObject.SetActive(false);
                        timer = Time.realtimeSinceStartup + waitSecond;
                        onAnimEnd?.Invoke();
                        onHide?.Invoke();
                    }
                }
            }
        }
    }
}